//
//  ListAlertView.m
//  Behing
//
//  Created by SHkingwelan on 2017/12/20.
//  Copyright © 2017年 lj. All rights reserved.
//

#import "ListAlertView.h"

#define ButtonTag 10086111

#import "ListAlertView.h"

@interface ListAlertView()
@property (nonatomic, strong) UIView *subBackView;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) NSMutableArray *buttonArray;
@property (nonatomic, assign) id <BaseProtocol> delegate;

@end

@implementation ListAlertView

- (NSMutableArray *)buttonArray {
    if (_buttonArray == nil) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

- (id _Nonnull )initWithdelegate:(id _Nonnull )delegate otherButtonTitles:(NSArray *_Nonnull)otherButtonTitles {
    if (![BHSettingUtil dataAndStringIsNull:otherButtonTitles]) {
        [self.buttonArray addObjectsFromArray:otherButtonTitles];
    }
    self.delegate = delegate;
    if (self = [self init]) {
    }
    return self;
}

- (id)init {
    if (self = [super init]) {
        [self creatView];
    }
    return self;
}

- (void)creatView {
    _subBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, App_Frame_Height)];
    _subBackView.backgroundColor = [UIColor blackColor];
    _subBackView.alpha = 0;
    [BHSettingUtil addTapGestureInView:_subBackView addTarget:self action:@selector(backClick) withTag:10086000];
    self.frame = _subBackView.bounds;
    [self addSubview:_subBackView];
    
    UIView *backView = [[UIView alloc]init];
    backView.center = CGPointMake(App_Frame_Width/2, App_Frame_Height/2);
    backView.bounds = CGRectMake(0, 0, App_Frame_Width - 60, 45*self.buttonArray.count);
    backView.backgroundColor = [UIColor whiteColor];
    [self addSubview:backView];
    _backView = backView;
    
    [self setUpUI];
}

- (void)setUpUI {
    if (self.buttonArray.count == 1) {
        return;
    }
    
    for (int i = 0; i < self.buttonArray.count; i ++) {
        UIButton *button = [[UIButton alloc]init];
        button.tag = ButtonTag + i;
        [button setTitle:self.buttonArray[i] forState:UIControlStateNormal];
        button.titleLabel.font = BHFont_Size_18;
        [button setTitleColor:Text_Color_333333 forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.backView addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backView.mas_left);
            make.right.equalTo(self.backView.mas_right);
            make.top.equalTo(self.backView.mas_top).offset(45*i);
            make.height.offset(45);
        }];
    }
}

#pragma mark - show
- (UIView *)topView {    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    return window;
}

- (void)presentContentView:(UIView *)view animated:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:0.5 animations:^{
            self.subBackView.alpha = 0.5;
            self.backView.alpha = 1;
        } completion:^(BOOL finished) {
        }];
        [[self topView]addSubview:self];
        [self showTransformAnimation];
    }else {
        _subBackView.alpha = 0.5;
        _backView.alpha = 1;
        [[self topView]addSubview:self];
    }
}

#pragma mark - Animation
//transform
- (void)showTransformAnimation {
    CAKeyframeAnimation *alertViewShowAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    alertViewShowAnimation.duration = 0.5;
    alertViewShowAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DIdentity]];
    alertViewShowAnimation.keyTimes = @[@0.2f,@0.5f,@0.75f,@1.0f];
    alertViewShowAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [_backView.layer addAnimation:alertViewShowAnimation forKey:nil];
}

#pragma mark - 取消视图
- (void)cancelView {
    [UIView animateWithDuration:0.5 animations:^{
        self.backView.alpha = 0.0;
        self.subBackView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - 按钮
//背景点击事件
- (void)backClick {
    [self cancelView];
}

- (void)buttonClick:(UIButton *)sender {
    [self cancelView];
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeColumnView:didSelect:)]) {
        [self.delegate homeColumnView:self didSelect:sender.tag - ButtonTag];
    }
}

@end
